/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：SysUserRole仓储                                              
*│　作    者：sys                                            
*│　创建时间：2020-11-06 14:41:01  
*│　说    明：代码生成器生成   
*└──────────────────────────────────────────────────────────────┘
*/
using NetModular.Lib.Utils.Core.Extensions;
using SXFramWork.Core;
using SXFramWork.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SXFramWork.Repository
{
    public class SysUserRoleRepository:RepositoryAbstract<SysUserRoleEntity>,ISysUserRoleRepository
    {
        public SysUserRoleRepository(IDbContext dbContext):base(dbContext)
        {
          
        }
        public async Task<List<SysRoleEntity>> QueryRole(string userId)
        {
            var list= await FromSql.GetDataListAsync<SysRoleEntity>(@"select r.id,r.Name,r.Remarks,r.CreateTime,'true' as selected from sysrole r left join SysUserRole ur on r.Id=ur.SysRoleId where SysUserId=@UId 
              union all
               select *,'false' as selected from SysRole", new { UId = userId });
             return list.Where(p => !list.Any(q => (p!= q && p.Id == q.Id&&q.Selected==true))).ToList();
        }

        public async Task<int> DeleteUserId(string userId)
        {
            return await FromSql.ExecuteAsync("delete  from sysuserrole where sysuserid=@uid", new { uid = userId });
        }

        public async Task<bool> ExistsRolesAsync(List<string> roleIds)
        {
            var count =await FromSql.GetStrAsync("select count(1) from sysuserrole where sysroleid in @ids", new { ids = roleIds });
            if (count.To_Int() <= 0)
                return false;
            return true;
        }
    }
}